import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {ModalDirective} from "ngx-bootstrap";
import {AliyunOssOutputUrl, tuikeSystemUrl} from "../../../config/api";
import {HttpService} from "../../lib/service/http.service";
import {Headers, RequestOptions} from "@angular/http";
import * as moment from 'moment';
import {Router} from "@angular/router";

@Component({
  selector: 'app-examine',
  templateUrl: './integral.component.html',
  styleUrls: ['./integral.component.scss']
})
export class IntegralComponent implements OnInit {
  aliyunOssOutputUrl = AliyunOssOutputUrl;
  searchData = '';

  type: string;
  message: string;
  isShow = true;
  totalItems: number;
  currentPage: number = 1;
  itemsPerPage: number = 20;
  _isSpinning = false;

  editItem = [];
  integralList = [];
  selectedOption;
  _startDate = null;
  _endDate = null;
  startDate = null;
  endDate = null;
  url = `${tuikeSystemUrl}/api/v1/admin/Task/jifen`;

  @ViewChild('detailModal') public detailModal: ModalDirective;


  _console(value) {
    console.log(value);
  }

  keyDown(e) {
    e.code === 'Enter' ? this.search() : '';
  }

  _startValueChange = () => {
    if (this._startDate > this._endDate) {
      this._endDate = null;
    }
  }
  _endValueChange = () => {
    if (this._startDate > this._endDate) {
      this._startDate = null;
    }
  }
  _disabledStartDate = (startValue) => {
    if (!startValue || !this._endDate) {
      return false;
    }
    return startValue.getTime() >= this._endDate.getTime();
  }
  _disabledEndDate = (endValue) => {
    if (!endValue || !this._startDate) {
      return false;
    }
    return endValue.getTime() <= this._startDate.getTime();
  }

  constructor(private httpService: HttpService, private el: ElementRef, private router: Router) {
  }

  ngOnInit() {
    this._startDate = new Date(moment(new Date().getTime()).format('YYYY-MM-DD') + ' 00:00:00');
    this.getIntegralList(1);
  }

  //重置
  reset() {
    this.searchData = '';
    this._startDate = new Date(moment(new Date().getTime()).format('YYYY-MM-DD') + ' 00:00:00');
    this._endDate = null;
    this.currentPage = 1;
    this.getIntegralList(1);
  }

  //搜索
  search() {
    this.getIntegralList(1);
  }

  closeMask() {
    this.el.nativeElement.querySelector('.mask').style.display = 'none';
  }

  showDetailModal(item) {
    this.editItem = item;
    this.detailModal.show();
  }

  getIntegralList(page) {
    this._isSpinning = true;
    this.startDate = this._startDate ? moment(this._startDate).format('YYYY-MM-DD 00:00:00') : '';
    this.endDate = this._endDate ? moment(this._endDate).format('YYYY-MM-DD 23:59:59') : '';
    let searchParams = '';
    this.searchData ? searchParams = '&key=' + this.searchData : '';
    this.httpService.httpGetAuth(this.url + '?start_date=' + this.startDate + searchParams ).subscribe(res => {
      if(res.code === 200) {
        this.integralList = res.data.member;
        this._isSpinning = false;
      } else {
        console.log(res);
        this._isSpinning = false;
      }
    })
  }

  pageChanged(event: any): void {
    window.scrollTo(0, 0);
    this.getIntegralList(event.page);
  }
}
